﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Portal.Web.Model;
using Portal.Web.Utility;
using Portal.Exam.Bussiness;
using Portal.Exam.Model;
using Portal.Core;

namespace Portal.Exam.Render
{
    public class HtmlEditor
    {
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
      
            try
            {
                WebSessionUtility.CheckSessionTimeOut(); 

                string QuestionId = (string)WebEnvironments.Request(SiteId, "QuestionId");
                QuestionCls questionCls = new QuestionBll().CreateModel(SiteId,QuestionId);
                if (questionCls == null) throw new Exception("Không tìm thấy câu hỏi. Truy cập không hợp lệ!");

                string RetHtml =
                    " <script language=javascript> \r\n" +
                    "     function ClientSaveHtmlEditor(QuestionId,ShowMsg) \r\n" +
                    "     { \r\n" +
                    "         SiteId='"+SiteId+"';\r\n"+
                    "         oEditor = FCKeditorAPI.GetInstance('FCKeditorBody');\r\n" +
                    "         bodyText= oEditor.GetXHTML( true ); \r\n" +
                    "         retValue=Portal.Exam.Render.HtmlEditor.ServerSideHtmlEditor(SiteId, QuestionId,bodyText).value; \r\n" +
                    "         if(retValue.indexOf(\"ERROR\")!=-1) \r\n" +
                    "         { \r\n" +
                    "            alert(retValue); \r\n" +
                    "            return; \r\n" +
                    "        } \r\n" +
                    "        if (ShowMsg == 1)  \r\n" +
                    "        { \r\n" +
                    "            alert(retValue); \r\n" +
                    "        } \r\n" +
                    "     } \r\n" +
                    " </script>\r\n" +
                    //" <div class><input type=button class=button value=\"Ghi lại\" onclick=\"javascript:ClientSaveHtmlEditor('" + questionCls.QuestionId + "',1);\"><input type=button class=button value=\"Quay lại\" onclick=\"javascript:window.open('ExamQuestion.aspx?GroupQuestionId="+questionCls.GroupQuestionId+"&MonthId="+questionCls.MonthiCls.MonthiId+"','_self');\"></div>\r\n" +

                     " <div class=DatabaseFunction>\r\n" +
                    "        <ul>\r\n" +
                    "            <li onclick=\"javascript:ClientSaveHtmlEditor('" + questionCls.QuestionId + "',1);\"><img class=image src=\"" + IconRoot + "/Save.png\"><div>Ghi lại</div></li>\r\n" +
                    //"            <li onclick=\"javascript:window.open('ExamQuestion.aspx?GroupQuestionId=" + questionCls.GroupQuestionId + "&MonthId=" + questionCls.MonthiCls.MonthiId + "','_self');\"><img class=image src=\"" + IconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                    "        </ul>\r\n" +
                    "  </div>\r\n" +

                    " <h3>Soạn thảo câu hỏi bằng HtmlEditor</h3>\r\n" +
                    " <textarea type=hidden id=\"FCKeditorBody\" name=\"FCKeditorBody\">" + questionCls.Question + "</textarea>\r\n" +
                    " <script type=\"text/javascript\" src=\"/fckeditor/fckeditor.js\"></script>\r\n" +
                    " <script type=\"text/javascript\">\r\n" +
                    "       var oFCKeditor = new FCKeditor('FCKeditorBody');\r\n" +
                    "       oFCKeditor.BasePath = \"/fckeditor/\";\r\n" +
                    "       oFCKeditor.Height = \"600\";\r\n" +
                    "       oFCKeditor.ReplaceTextarea();\r\n"+
                    " </script>\r\n";
                   
                OAjaxOut.HtmlContent = RetHtml;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public string GetPreviewUrl(string SiteId,string QuestionId)
        {
            string previewUrl = "PreviewOneQuestion.aspx?QuestionId="+QuestionId;
            return previewUrl;
        }

        [AjaxPro.AjaxMethod]
        public string GetConfirmSave(string SiteId)
        {
            return "Bạn có muốn ghi lại trước khi xem không?";
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideHtmlEditor(string SiteId, string QuestionId, string QuestionText)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut(); 
                new QuestionBll().SaveQuestionText(null, SiteId, QuestionId, QuestionText);
                return "Ghi lại câu hỏi = html thành công!";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }
    }
}